﻿#ifndef PRODUCTSERVICE_H
#define PRODUCTSERVICE_H

#include <QObject>
#include "productnetworkaccessmanager.h"
#include "producttableview.h"
#include "producttreeview.h"
#include "excelimportthread.h"
#include "formula.h"
#include "userrelation.h"
#include "dingdandetail.h"
#include <QPushButton>

class ProductService : public QObject
{
    Q_OBJECT
public:
    explicit ProductService(QObject *parent = nullptr);

    void addProduct(
            QWidget *widget,
            QPushButton *button,
            QString type,
            QString name,
            QString unit,
            QString productNum,
            QString upperLimit,
            QString lowerLimit,
            QString brand,
            QString originPlace,
            QString specifications,
            QString barCode,
            QString remark,
            QString guidePrice1,
            QString guidePrice2,
            QString primaryDirectory,
            QString secondaryDirectory);

    void updateProduct(
            QWidget *widget,
            QString productId,
            QString type,
            QString name,
            QString unit,
            QString productNum,
            QString upperLimit,
            QString lowerLimit,
            QString brand,
            QString originPlace,
            QString specifications,
            QString barCode,
            QString remark,
            QString guidePrice1,
            QString guidePrice2,
            QString primaryDirectory,
            QString secondaryDirectory);

    /**
     * 刷新商品，并同步到本地
     * @brief refreshProducts
     * @param widget
     * @param productIds
     */
    void refreshProducts(
            QWidget *widget,
            QString productIds);

    void updateProductData(QWidget *widget, ProductTableView *tableView);

    void updateProductData();

    void queryProducts(
            ProductTableView *tableView,
            ProductTreeView *treeView,
            int limitIndex,
            QString overallQuery,
            QString typeQuery,
            QString nameQuery);

    void uploadProduct(
            QWidget *widget,
            QList<Product*> products);

    void uploadProductFinished(QWidget *widget, double totalMoney);

    bool checkDataHeader(QList<QVariant> header);

    Product* checkData(int index, QList<QVariant> dataItem);

    QList<Product*> checkData(QList<QList<QVariant>> data);

    void addFormula(QWidget *widget, Product *combineProduct, int combineCount, QList<Product*> splitProducts);

    void deleteFormula(QWidget *widget, QString combineId);

    void updateFormulaData(QWidget *widget);

    void combineProduct(QWidget *widget, QPushButton *button, Formula *formula, int combineCount);

    void splitProduct(QWidget *widget, QPushButton *button, Formula *formula, int splitCount, QString splitInPrices);

    void countProduct(QWidget *widget, QPushButton *button, QList<Product*> products);

    void sanbaoMove(QWidget *widget, QPushButton *button, QList<Product*> products, Product::SanBaoMoveType moveType);

    void updateHistoryProduct(QWidget *widget, QList<HistoryProduct*> historyProducts, int type);

    void getAllHistoryProdycts(
            QWidget *widget,
            UserRelation *relation,
            QList<DingdanDetail*> details);

    void deleteProduct(QWidget *widget, Product *product);

    void getFormulaInfo(QWidget *widget, QString combineId);
signals:
    void addProductSuccess();

    void countProductSuccess();

    void sanbaoMoveSuccess();

    void getAllHistoryProdyctsSuccess(QList<HistoryProduct*> historyProducts);

    void importProductSuccess(QList<Product*> products);

    void uploadProductsSuccess();

    void updateFormulaSuccess(QList<Formula*> formulas);

    void deleteFormulaSuccess();

    void getFormulaInfoSuccess(Formula *formula);

public slots:

private:
    ProductNetworkAccessManager *manager;

    ExcelImportThread *importThread;
};

#endif // PRODUCTSERVICE_H
